package org.wsjc.common.solr;

import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wsjc.common.core.utils.DateUtils;
import com.wsjc.manager.entity.vo.PcSterilizationVo;

public class SolrJTes {
	
	private static Logger logger = LoggerFactory.getLogger(SolrJTes.class);

	@Test
	public void addDocument() throws Exception {
		
		try {
			//创建一连接
			SolrServer solrServer = new HttpSolrServer("http://47.104.198.2:8081/solr");
			
			PcSterilizationVo vo = new PcSterilizationVo();
			vo.setHospitalId("0000000000000000001");
			vo.setCoolVolatility("coolVolatility");
			vo.setHospitalName("大连医大二医院");
			vo.setHospitalType("医院");
			vo.setAreaId("110101");
			vo.setDetectionResult("y");
//			vo.setDetectionTime(new Date());
			vo.setDataType("miejun");
			
			vo.setOpPersion("李阳");
			vo.setOpDepartment("大连东软信息学院");
			
			solrServer.addBean(vo) ;
			
			SolrInputDocument document = new SolrInputDocument();
			document.addField("id", "0000000000000000001");
			document.addField("hospital_name", "大连医大二医院");
			document.addField("hospital_type", "医院");
			document.addField("data_type", "miejun");
			document.addField("area_id", "110101");
			document.addField("detection_result", "y");
			document.addField("detection_time", new Date());
			document.addField("hospital_id", "hosid99999999999");
			document.addField("op_persion", "李阳");
			document.addField("op_department", "大连东软信息学院");
			
			//把文档对象写入索引库
			solrServer.add(document); 
			
			//提交
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void deleteDocument() throws Exception {
		//创建一连接
		SolrServer solrServer = new HttpSolrServer("http://47.104.198.2:8081/solr");
		solrServer.deleteById("Wj00S110101201808131");
//		solrServer.deleteByQuery("*:*");
		solrServer.commit();
	}
	
	@Test
	public void queryDocument() throws Exception {
		
		long starTime = System.currentTimeMillis();
		
		SolrServer solrServer = new HttpSolrServer("http://47.104.198.2:8081/solr");
		// 创建一个查询对象
		SolrQuery query = new SolrQuery();
		// 设置查询条件
		query.setStart(1);
		query.setRows(10);
		query.set("df", "data_keywords");
		

		/** 时间段 **/
//		System.out.println(DateUtils.getTimeStampStr(DateUtils.addDay(DateUtils.addHour(new Date(), 8), -4)));
//		String time = "detectionTime:["+DateUtils.parseDateToGNLZ(DateUtils.addDay(DateUtils.addHour(new Date(), 8), -4))+" TO "+DateUtils.parseDateToGNLZ(DateUtils.addHour(new Date(), 8))+"]";  
//		System.out.println(time);
//		System.out.println(DateUtils.getTimeStampStr(DateUtils.addHour(new Date(), 8)));
//		query.setQuery("hospitalType:医院 AND hospitalName:天津 AND detectionResult:2 AND "+ time);  // detectionTime:[2018-01-28T00:00:00Z TO 2018-08-28T23:59:59.999Z]"
		
		
		String datetime = "detectionTimestamp:["+DateUtils.addDay(new Date(), -100).getTime()+" TO "+new Date().getTime()+"]";
		System.out.println(DateUtils.getTimeStampStr(DateUtils.addDay(new Date(), -100)));
		System.out.println(DateUtils.getTimeStampStr(new Date()));
//		query.setQuery("hospitalType:医院 AND hospitalName:北京 AND detectionResult:1 AND "+datetime);  // detectionTime:[2018-01-28T00:00:00Z TO 2018-08-28T23:59:59.999Z]"
		query.setQuery("hospitalName:北京 AND detectionResult:1 AND "+datetime);  // detectionTime:[2018-01-28T00:00:00Z TO 2018-08-28T23:59:59.999Z]"
		
		// 用版本号--控制时间倒叙
		query.setSort("detectionTimestamp", ORDER.desc);//排序：名称倒序 
		
		try {
			// 执行查询
			QueryResponse response = solrServer.query(query);
			// 取查询结果
			List<PcSterilizationVo> sterilizations = response.getBeans(PcSterilizationVo.class);
			logger.info("共查询到记录：" + response.getResults().getNumFound());
			
			for(PcSterilizationVo pcSterilizationVo: sterilizations){
				logger.info(pcSterilizationVo.toString());
			}
			
			long Time = System.currentTimeMillis() - starTime;
			logger.info("Sterilization 保存Successful|一共耗时:{}秒" , (double) Time / 1000);
		} catch (Exception e) {
			logger.error("查询solr失败：" , e);
		}
	}
	
	
	
	
	
	
	
}
